package com.leyou.user.service.controller;

import com.leyou.common.annotation.ServiceLog;
import com.leyou.common.bean.PageResult;
import com.leyou.common.bean.Result;
import com.leyou.user.inter.dto.*;
import com.leyou.user.inter.pojo.Role;
import com.leyou.user.inter.pojo.User;
import com.leyou.user.inter.vo.UserPageVO;
import com.leyou.user.service.service.AuthorizeService;
import com.leyou.user.service.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author zqq
 * @ClassName UserController
 * @Description
 * @date 2020/3/4-16:36
 */
@RestController
@Api(value = "UserController",description = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AuthorizeService authorizeService;

    @GetMapping("check/{data}/{type}")
    @ApiOperation(value = "检查用户是否可用", notes = "检查用户是否可用")
    @ServiceLog(operateType = "检查用户是否可用",operateContent = "检查用户是否可用")
    public Result<Boolean> checkUserData(
            @ApiParam(name = "data",value = "要校验的数据",required = true) @PathVariable("data") String data,
            @ApiParam(name = "type",value = "要校验的数据类型：1，用户名；2，手机",required = true) @PathVariable(value = "type") Integer type) {
        return this.userService.checkData(data, type);
    }

    @GetMapping("code/{phone}/{type}")
    @ApiOperation(value = "发送手机验证码", notes = "发送手机验证码")
    public Result<Boolean> sendVerifyCode(
            @ApiParam(name = "phone",value = "手机号",required = true) @PathVariable(value = "phone") String phone,
            @ApiParam(name = "type",value = "验证码发送类型：0/注册、1/登录",required = true) @PathVariable(value = "type") Integer type) {
        return this.userService.sendVerifyCode(phone,type);
    }

    @PostMapping("register")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public Result<Boolean> register(@Valid @RequestBody UserRegisterDTO dto) {
        return this.userService.register(dto);
    }

    @PostMapping("login")
    @ApiOperation(value = "登录接口(内部服务访问)", notes = "登录接口")
    public Result<User> queryUser(@RequestBody UserLoginDTO dto) {
        return this.userService.queryUser(dto);
    }

    @PostMapping("user/page")
    @ApiOperation(value = "分页查询用户列表", notes = "分页查询用户列表")
    public Result<PageResult<UserPageVO>> queryUserByPage(@RequestBody UserPageDTO dto) {
        return this.userService.queryUserByPage(dto);
    }

    @PostMapping("user")
    @ApiOperation(value = "后台添加用户", notes = "后台添加用户")
    public Result addUser(@RequestBody UserAddDto dto) {
        return this.userService.addUser(dto);
    }

    @DeleteMapping("user")
    @ApiOperation(value = "删除和恢复用户", notes = "删除和恢复用户")
    public Result deleteUser(@RequestBody UserUpdateDTO dto) {
        return this.userService.deleteUser(dto);
    }

    @PutMapping("locked")
    @ApiOperation(value = "锁定或解锁用户", notes = "锁定或解锁用户")
    public Result lockedUser(@RequestBody UserUpdateDTO dto) {
        return this.userService.lockedUser(dto);
    }

    @PutMapping("reset")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码")
    public Result resetUser(@RequestBody UserUpdateDTO dto) {
        return this.userService.resetUser(dto);
    }

    @PutMapping("update/role")
    @ApiOperation(value = "修改用户角色", notes = "修改用户角色")
    public Result updateUserAndRole(@RequestBody UserUpdateDTO dto) {
        return this.userService.updateUserAndRole(dto);
    }

    @GetMapping("query/user/info")
    @ApiOperation(value = "登录成功获取用户信息", notes = "登录成功获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result<UpdateMyInfo> queryUserInfo() {
        return this.userService.queryUserInfo();
    }

    @PutMapping("update/myinfo")
    @ApiOperation(value = "用户信息修改", notes = "用户信息修改")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result updateMyInfo(@RequestBody UpdateMyInfo dto) {
        return this.userService.updateMyInfo(dto);
    }

    @GetMapping("role/all")
    @ApiOperation(value = "查询角色", notes = "查询角色")
    public Result<List<Role>> queryRoleByAll() {
        return this.authorizeService.queryRoleByAll();
    }

    @GetMapping("old/send/code")
    @ApiOperation(value = "绑定手机，旧手机发送验证码", notes = "绑定手机，旧手机发送验证码")
    public Result sendOldSMS() {
        return this.userService.sendOldSMS();
    }

    @GetMapping("old/verify/code/{code}")
    @ApiOperation(value = "绑定手机，验证旧手机验证码", notes = "绑定手机，验证旧手机验证码")
    public Result verifyOldCode(@ApiParam(name = "code",value = "验证码",required = true) @PathVariable(value = "code") String code) {
        return this.userService.verifyOldCode(code);
    }

    @GetMapping("new/send/code/{phone}")
    @ApiOperation(value = "绑定手机，新手机发送验证码", notes = "绑定手机，新手机发送验证码")
    public Result sendNewSMS(@ApiParam(name = "phone",value = "手机号",required = true) @PathVariable(value = "phone") String phone) {
        return this.userService.sendNewSMS(phone);
    }

    @GetMapping("new/verify/code/{phone}/{code}")
    @ApiOperation(value = "绑定手机，验证新手机验证码并修改用户绑定的手机号", notes = "绑定手机，验证新手机验证码并修改用户绑定的手机号")
    public Result verifyNewCode(@ApiParam(name = "phone",value = "手机号",required = true) @PathVariable(value = "phone") String phone,
                                @ApiParam(name = "code",value = "验证码",required = true) @PathVariable(value = "code") String code) {
        return this.userService.verifyNewCode(phone,code);
    }

}
